﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.Service;
using FantastPlatform.Framework;
using NHibernate;
using NHibernate.Criterion;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Decision.Model;
using FantastPlatform.Decision.IService;

namespace FantastPlatform.Decision.Service
{
    public class MaintainStabilityPersonService : DataService<IMaintainStabilityPerson, MaintainStabilityPerson>, IMaintainStabilityPersonService
    {
        public override void Update(IMaintainStabilityPerson entity)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {

                entity.ModifyDate = DateTime.Now;
                entity.Enable = true;
                Session.Merge(entity);
            }, transaction);
        }

        public IList<IMaintainStabilityPerson> SearchMaintainStability(int policeId)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(MaintainStabilityPerson));

            criteria.Add(Expression.Eq("Enable", true))
                .Add(Expression.Between("CreateDate", DateTime.Now.Date.AddDays(-3), DateTime.Now.AddHours(1)));

            criteria.CreateCriteria("Person", "p")
            .Add(Expression.Eq("p.Id", policeId));

            criteria.CreateCriteria("MaintainStability", "m")
            .Add(Expression.Eq("m.Enable", true));
            criteria.AddOrder(Order.Desc("CreateDate"));
            return criteria.List<IMaintainStabilityPerson>();
        }


        public IList<IMaintainStabilityPerson> SearchMaintainStability(string policeCode)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(MaintainStabilityPerson));

            criteria.Add(Expression.Eq("Enable", true))
                .Add(Expression.Between("CreateDate", DateTime.Now.Date.AddDays(-3), DateTime.Now.AddHours(1)));

            criteria.CreateCriteria("Person", "p")
            .Add(Expression.Eq("p.Code", policeCode));

            criteria.CreateCriteria("MaintainStability", "m")
            .Add(Expression.Eq("m.Enable", true));
            criteria.AddOrder(Order.Desc("CreateDate"));
            return criteria.List<IMaintainStabilityPerson>();
        }
    }
}
